﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using jSLCore.Client.Presentation.ApplicationContainer;

namespace jSLApp.Client.Controls
{
    public partial class jSLBrowser : UserControl
    {
        private HtmlElement _frame;
        private bool IsIFrameVisible;
        private Uri currentUri;

        public jSLBrowser()
        {
            InitializeComponent();

            _frame = HtmlPage.Document.GetElementById("brFrame");

            Loaded += (o, eventArgs) =>
                          {
                              CheckVisibility();
                                     UpdateSize();
                                 };
            LayoutUpdated+=OnLayoutUpdated;
            
            Unloaded += (sender1, routedEventArgs) =>
                            {
                                SetSource("about:blank");
                                HideBrowser();
                            };

        }

        private void HideBrowser()
        {
            IsIFrameVisible = false;
            
            _frame.SetStyleAttribute("visibility", "hidden");
        }

        public void SetSource(string uri)
        {
            SetSource(new Uri(uri));
        }
        public void SetSource(Uri uri)
        {
            if (currentUri != uri)
            {
                currentUri = uri;
                _frame.SetAttribute("src", uri.ToString());
            }
        }

        private void OnLayoutUpdated(object sender, EventArgs eventArgs)
        {
            try
            {
                UpdateSize();


                CheckVisibility();
            }
            catch (Exception)
            {
             //уже и забыл зачем тут делал catch, но оставлю, сейчас не до этого   
            }
        }

        private void CheckVisibility()
        {
            bool hide = true;

            if(Visibility==Visibility.Visible)
                if(ApplicationContainer.CurrentContentContainer.ParentApplicationContainer.Dialog==null)
                {
                    hide = false;
                }

            if(hide)HideBrowser();
            else ShowBrowser();
        }

        private void UpdateSize()
        {
            GeneralTransform gt =
                TransformToVisual(Application.Current.RootVisual);
            Point offset = gt.Transform(new Point(0, 0));

            int controlTop = (int) offset.Y;
            int controlLeft = (int) offset.X;

            _frame.SetStyleAttribute("left", controlLeft + "px");
            _frame.SetStyleAttribute("top", controlTop + "px");
            _frame.SetStyleAttribute("width", ActualWidth + "px");
            _frame.SetStyleAttribute("height", ActualHeight + "px");
            
           
        }

        private void ShowBrowser()
        {
            _frame.SetStyleAttribute("visibility", "visible");
            IsIFrameVisible = true;
        }
    }
}
